#include <windows.h>
#include <shobjidl.h>
#include <string>

// 函数用于弹出文件选择对话框，限定只选择文件夹，并返回选择的文件夹路径
std::wstring BrowseFolder()
{
    std::wstring folderPath;
    
    // 创建IFileOpenDialog对象
    IFileOpenDialog* pFileOpen = NULL;
    HRESULT hr = CoCreateInstance(
        CLSID_FileOpenDialog,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&pFileOpen)
    );
    
    if (SUCCEEDED(hr))
    {
        // 设置文件选择对话框的选项
        FILEOPENDIALOGOPTIONS options;
        pFileOpen->GetOptions(&options);
        pFileOpen->SetOptions(options | FOS_PICKFOLDERS);
        
        // 显示文件选择对话框
        hr = pFileOpen->Show(NULL);
        
        // 获取用户选择的文件夹路径
        if (SUCCEEDED(hr))
        {
            IShellItem* pItem;
            hr = pFileOpen->GetResult(&pItem);
            
            if (SUCCEEDED(hr))
            {
                PWSTR pszFilePath;
                hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
                
                if (SUCCEEDED(hr))
                {
                    folderPath = pszFilePath;
                    CoTaskMemFree(pszFilePath);
                }
                
                pItem->Release();
            }
        }
        
        pFileOpen->Release();
    }
    
    return folderPath;
}

int main()
{
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    
    // 弹出文件选择对话框，只选择文件夹，并获取文件夹路径
    std::wstring selectedFolder = BrowseFolder();
    
    if (!selectedFolder.empty())
    {
        // 处理获取到的文件夹路径
        // ...
    }
    
    CoUninitialize();
    
    return 0;
}